<?php

namespace app\admin\controller;

use app\common\Helper;
use app\admin\model\Member;
use app\admin\model\User as UserMdl;

use think\Db;
use think\Exception;
use think\Request;

/**
 * 分销会员
 */
class User extends Base
{

    public function _initialize()
    {
        parent::_initialize();

        $this->model = new Member();
    }

    /**
     * 列表
     */
    public function getList(){
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;

        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        //$defaultWhere = $this->getDefaultWhere();
        $defaultWhere = [];
        $defaultWhere['a.status'] = ['neq', 2];
        $defaultWhere['a.phone'] = [['neq', ''], ['neq', 'null'], 'and'];
        $where = $defaultWhere;
        if (isset($param->name)) {
            $where['a.name'] = ['like', '%' . $param->name . '%'];
        }
        $model = $this->model;
        $join = [];
        $field = 'a.*';
        $order = 'a.createDate DESC';
        $result = $model->alias('a')->where($where)->join($join)->field($field)->limit($start, $length)->order($order)->select();
        $total = $model->alias('a')->where($where)->join($join)->count();

        if (!$result) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '没有相关分销会员');
        }

        foreach($result as $item){
            $userInfo = UserMdl::where(['id' => $item['id']])->find();
            $item['userAvatar'] = $userInfo['imgUrl'];
            if($item['boss']){
                $item['boss'] = Member::where(['id' => $item['boss']])->value('name');
            }
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result, $total);
    }

    /**
     * 删除
     */
    public function delete(){
        $param = self::getHttpParam();
        $model = $this->model;

        if (empty($param->uId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'uId 不能为空');
        }

        $id = $param->uId;
        Db::startTrans();
        try {
            //$result = $model->where('id', $id)->delete();
            $result = $model->where('id', $id)->update(['status' => 2]);
            if($result !== false){
                // Query执行后的操作
                $model->_after_delete($id);

                Db::commit();
                return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, '删除成功');
            }else{
                Db::rollback();
                return show(config('status.ERROR_STATUS'), self::MSG_ERROR, $model->getError());
            }
        } catch (Exception $e) {
            Db::rollback();
            return show(config('status.ERROR_STATUS'), self::MSG_ERROR, $e->getMessage());
        }
    }

    /**
     * 启用/禁用
     */
    public function handle(){
        $param = self::getHttpParam();
        $model = $this->model;

        if (empty($param->uId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'uId 不能为空');
        }
        if (empty($param->type)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'type 不能为空');
        }
        if (!in_array($param->type, [1, 2, 3])) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'type 格式不正确');
        }

        $id = $param->uId;
        //$info = $model->where('id', $id)->find();
        if($param->type == 1){
            // 通过
            $data['status'] = 1;
            $result = $model->allowField(true)->save($data, ['id' => $id]);
        }else if($param->type == 2){
            // 启用
            $data['status'] = 1;
            $result = $model->allowField(true)->save($data, ['id' => $id]);
        }else{
            // 禁用
            $data['status'] = 4;
            $result = $model->allowField(true)->save($data, ['id' => $id]);
        }

        if($result !== false){
            return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, '操作成功');
        }else{
            return show(config('status.ERROR_STATUS'), self::MSG_ERROR, $model->getError());
        }
    }

}